/**
 * 文件：InsertAccomPanel.java
 * 说明：用于添加住宿信息
 * 作者：古通基
 * 时间：2011-09-14
 */

package com.java.component;

import java.sql.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class InsertAccomPanel extends JPanel implements ActionListener,MouseListener
{
	private static final long serialVersionUID = -9062553557971000185L;
	private JLabel lno = new JLabel("学号：");                                          //静态标签
	private JLabel lname = new JLabel("姓名：");
	private JLabel lroom = new JLabel("宿舍：");              
	private JLabel lbed = new JLabel("床位：");        
	
	private JTextField no = new JTextField("请输入五位学号",10);              //文本输入
	private JTextField name = new JTextField("",10); 
	String[] rooms = {"6425","6426","6427","3625","3626","3627"};
    private JComboBox room = new JComboBox(rooms);    
    String[] beds = {"1","2","3","4","5","6","7","8"};
    private JComboBox bed = new JComboBox(beds);    
    
    private JButton insert = new JButton("添加");                                    //声明按钮
    private JButton reset = new JButton("重置");
    private JButton query = new JButton("查询");
    
    private JPanel JPcomon1 = new JPanel();                                          //声明面板
    private JPanel JPcomon2 = new JPanel();
    private JPanel JPcomon3 = new JPanel();
    private JPanel JPcomon4 = new JPanel();
    private JPanel JPbutton = new JPanel();
    
    public InsertAccomPanel()                                                                 //构造函数
    {
    	lno.setHorizontalAlignment(SwingConstants.CENTER);
    	lname.setHorizontalAlignment(SwingConstants.CENTER);
    	lroom.setHorizontalAlignment(SwingConstants.CENTER);
    	lbed.setHorizontalAlignment(SwingConstants.CENTER);

    	JPcomon1.setLayout(new FlowLayout(FlowLayout.LEFT,12,5));
    	JPcomon1.add(lno);
    	JPcomon1.add(no);
    	JPcomon1.add(query);
    	JPcomon2.setLayout(new FlowLayout(FlowLayout.LEFT,12,5));
    	JPcomon2.add(lname);
    	JPcomon2.add(name);
    	JPcomon3.setLayout(new FlowLayout(FlowLayout.LEFT,12,5));
    	JPcomon3.add(lroom);
    	JPcomon3.add(room);
    	JPcomon4.setLayout(new FlowLayout(FlowLayout.LEFT,12,5));
    	JPcomon4.add(lbed);
    	JPcomon4.add(bed);    	
    	JPbutton.setLayout(new FlowLayout(FlowLayout.CENTER,12,5));
    	JPbutton.add(insert);
    	JPbutton.add(reset);
    	
    	this.setLayout(new GridLayout(10,1));
    	this.add(JPcomon1);
    	this.add(JPcomon2);
    	this.add(JPcomon3);
    	this.add(JPcomon4);
    	this.add(JPbutton);
    	
    	insert.addActionListener(this);                                                         //事件监听        
    	reset.addActionListener(this);
    	query.addActionListener(this);
    	no.addMouseListener(this);
    }    
    
	public void actionPerformed(ActionEvent e)
	{
		if(e.getActionCommand().equals("添加"))
		{
			AddToDataBase();
		}
		else if(e.getActionCommand().equals("重置"))
		{
			no.setText("");
			no.setEditable(true);
		}
		else if(e.getActionCommand().equals("查询"))
		{
			QueryDataBase();
			no.setEditable(false);		
			name.setEditable(false);
		}
	}
	
	private void AddToDataBase()
	{
		try
		{
			BaseSQL accom = null;
			accom = new BaseSQL();
			String sql = "insert into accom values('"+Integer.parseInt(no.getText())+"','"+name.getText()+"','"+room.getSelectedItem()+"','"+bed.getSelectedItem()+"')";
		    accom.executeDML(sql);
		    JOptionPane.showMessageDialog(this, "成功添加学号为："+no.getText()+" 的住宿信息", "住宿添加",JOptionPane.INFORMATION_MESSAGE);
		}
		catch(SQLException evt)
		{
			JOptionPane.showMessageDialog(this, evt, "系统错误",JOptionPane.INFORMATION_MESSAGE);
		} 
		catch (ClassNotFoundException evt) 
		{
			JOptionPane.showMessageDialog(this, evt, "系统错误",JOptionPane.INFORMATION_MESSAGE);
		}
		catch (NumberFormatException evt) 
		{
			JOptionPane.showMessageDialog(this, evt, "系统错误",JOptionPane.INFORMATION_MESSAGE);
		}
	}
	
	private void QueryDataBase()
	{		
		ResultSet rs = null;
		try
		{
			BaseSQL stu = null;	
			stu = new BaseSQL();
			String sql = "select * from student where sno = '"+Integer.parseInt(no.getText())+"'"; 
			
		    rs = stu.executeSelect(sql);
		    if(rs.next())
    		{
    			if(Integer.parseInt(no.getText())==rs.getInt("sno"))
    			{
    				name.setText(rs.getString("sname"));		
        		}    				
       		} 
    		else
    		{
    			JOptionPane.showMessageDialog(null,"用户不存在！","登录错误",JOptionPane.INFORMATION_MESSAGE);
    		}
		}
		catch(SQLException evt)
		{
			JOptionPane.showMessageDialog(this, evt, "系统错误",JOptionPane.INFORMATION_MESSAGE);
		} 
		catch (ClassNotFoundException evt) 
		{
			JOptionPane.showMessageDialog(this, evt, "系统错误",JOptionPane.INFORMATION_MESSAGE);
		}
		catch (NumberFormatException evt) 
		{
			JOptionPane.showMessageDialog(this, evt, "系统错误",JOptionPane.INFORMATION_MESSAGE);
		}
	}
	
    public void mouseClicked(MouseEvent e) { }                                //鼠标事件
    public void mouseReleased(MouseEvent e) { }
    public void mouseEntered(MouseEvent e) 
    {
    	if(no.getText().equals("请输入五位学号"))
    	{    		
    		no.setText("");
    		this.setVisible(true);
    	}
    }
    public void mousePressed(MouseEvent e) { }
    public void mouseExited(MouseEvent e) { }
}
